//
// Author    : Pablo Bosch 
// Contact   : pboschvivancos@epo.org
// Last Modif: 13/03/2005
//


// global vars 

var MOD_FTR = {};

MOD_FTR.CLSFILTERS=["List","All","Paid","Searched","Independent","Unsearched","No eq","Find"];

MOD_FTR.GRPFILTERS= [];

MOD_FTR._CLSSHOW= [];
MOD_FTR._CLSSHOWLENGTH=0;
MOD_FTR._GRPSHOW= [];

MOD_FTR._selgrpfilter="";
MOD_FTR._selfilter="_List";
MOD_FTR._selfilterfind="";
MOD_FTR._selfilterclaims="All";
MOD_FTR._changedfilters=true;
MOD_FTR._callback="";

MOD_FTR._prev_selfilter="";
MOD_FTR._prev_selfilterfind="";
MOD_FTR._prev_selfilterclaims="";

function SetFilterClaims(clist) {
  MOD_FTR._prev_selfilterclaims=MOD_FTR._selfilterclaims;
  MOD_FTR._selfilterclaims=clist;    
}

function GetFilterClaims() {
  return MOD_FTR._selfilterclaims;    
}

function SetFilterChanged(val) {
  MOD_FTR._changedfilters=val;
}

function IsFilterChanged() {
  return MOD_FTR._changedfilters;
}

function GetFilterClaims() {
  return MOD_FTR._selfilterclaims;
}

function GetFilterGroup() {
  return MOD_FTR._selgrpfilter;
}

function SetFilterGroup(grp) {
  MOD_FTR._selgrpfilter=grp;
}

function SetFilterFind(txt) {
  MOD_FTR._prev_selfilterfind=MOD_FTR._selfilterfind;
  MOD_FTR._selfilterfind=txt;
}

function GetFilterFind() {
  return MOD_FTR._selfilterfind;
}

function GetFilter() {
  return MOD_FTR._selfilter || "List";
}

function IsClaimInFilter(cl) {
  return MOD_FTR._CLSSHOW[cl];
}

function IsClaimInFilterGroup(cl) {
  return !GetFilterGroup() || GetClaimGroup(cl)==GetFilterGroup();
}


MOD_FTR._SetFilter=function(filter) {
  if(firstchar(filter)!="_"){
    MOD_FTR._prev_selfilter=MOD_FTR._selfilter;
  }
  MOD_FTR._selfilter=filter;
}

function SetFilter(filter,args) {
  var _NEWCLSSHOW=[];
  MOD_FTR._CLSSHOWLENGTH=0;
  var _clnum = GetClaimSetLength(); 
  var cllist = new List(); 
  
  switch(filter) {
  case "All":
    if(GetGroupsLength()>1) {
      for(var cl=1; cl <= _clnum ; cl++) {
	if(IsClaimInFilterGroup(cl)) {
	  _NEWCLSSHOW[cl]=1;
	  MOD_FTR._CLSSHOWLENGTH++;
	  cllist.push(cl);
	} 
      }
    } else {
      for(var cl=1; cl <= _clnum ; cl++) {
	_NEWCLSSHOW[cl]=1;
	MOD_FTR._CLSSHOWLENGTH++;
	cllist.push(cl);
      }
      //alert(cllist.getValue());
    }
    break;

  case "Paid":
    if(GetGroupsLength()>1) {
      for(var cl=1; cl <= _clnum ; cl++) {
	if(IsGroupPaid(GetGroupIdx(GetClaimGroup(cl)))) {
	  _NEWCLSSHOW[cl]=1;
	  MOD_FTR._CLSSHOWLENGTH++;
	  cllist.push(cl);
	} 
      }
    } else {
      return SetFilter("All");
    }
    break;

  case "Searched":
    for(var cl=1; cl <= _clnum ; cl++) {
      //alert("Claim value is "+cl+" = "+GetClaimValue(cl));
      if(IsClaimSearched(cl)&&IsClaimInFilterGroup(cl)){
	_NEWCLSSHOW[cl]=1;
        MOD_FTR._CLSSHOWLENGTH++;
        cllist.push(cl);
      }
    }
  break;

  case "Independent": 
    for(var cl=1; cl <= _clnum ; cl++) {
      if(is_clindependent(cl)&&IsClaimInFilterGroup(cl)){
	_NEWCLSSHOW[cl]= 1;
        MOD_FTR._CLSSHOWLENGTH++;
	cllist.push(cl);
      }
    }
    break;

  case "No eq": 
    for(var cl=1; cl <= _clnum ; cl++) {
      if(IsClaimInFilterGroup(cl) && GetClaimEq(cl)==""){
	_NEWCLSSHOW[cl]= 1;
        MOD_FTR._CLSSHOWLENGTH++;
	cllist.push(cl);
      }
    }
    break;
    
  case "Unsearched":
    for(var cl=1; cl <= _clnum ; cl++) {
      if(!IsClaimSearched(cl) && IsClaimInFilterGroup(cl)){
	_NEWCLSSHOW[cl]=1;
        MOD_FTR._CLSSHOWLENGTH++;
	cllist.push(cl);
      }
    }    
    break;
    
  case "Find":
    SetFilterFind(args);      
    if(args) {
      // case insensitive search
      var myregexp = new RegExp(args,"i");
      for(var cl=1; cl <= _clnum ; cl++) {
	if(IsClaimInFilterGroup(cl)) {
	  if(GetClaimDescription(cl).match(myregexp)) {
	    _NEWCLSSHOW[cl]=1;
	    MOD_FTR._CLSSHOWLENGTH++;
	    cllist.push(cl);
	  }
	}
      }
    } else {
      MOD_FTR._selfilter=filter;      
      return;
    }
    break; 
    
  case "List":   
    var cl_iter = new List_iterator(list_toList(args));
    while(!cl_iter.end()) {
      var cl=cl_iter.getValue();
      if(cl <= _clnum) {   // check input is in bounds 
        if(IsClaimInFilterGroup(cl)) { 
	  _NEWCLSSHOW[cl]=1;
	  MOD_FTR._CLSSHOWLENGTH++;
	  cllist.push(cl);
        }
      }
      cl_iter.next();
    }
    break;
  }

  // did something change ??
  
  MOD_FTR._SetFilter(filter);  
  SetFilterClaims(cllist.getValue());
  
  SetFilterChanged(true);
  MOD_FTR._CLSSHOW=_NEWCLSSHOW;
}





MOD_FTR._filtercallback=function(selopt,args,refresh) {      
  //alert("called "+callfunc+" "+selopt);
  if(!selopt) {
    selopt=html_selected("selfilter",MOD_FTR.CLSFILTERS);
  }
  switch(selopt) {
  case "List":
    var obj=document.getElementById("o1");
    if(obj) {
      obj.value=List_tolist(GetFilterClaims());
    }
    MOD_FTR._SetFilter(selopt);
    break;
  case "Find":    
    var obj=document.getElementById("f1");
    html_findstr_reset();
    obj.value=GetFilterFind();    
    MOD_FTR._SetFilter(selopt);
    SetFilterChanged(true);   
    break;
  case "_Find": // first click on find
    var obj=document.getElementById("f1");
    if(args) {
      SetFilter("Find",args);
    } else {
      SetFilter("Find",obj.value);     
    } 
    obj.value=GetFilterFind();
    break;
  case "_List": // changed list value
    var obj=document.getElementById("o1"); 
    if(args) {
      SetFilter("List",args);
    } else {
      SetFilter("List",obj.value);
    } 
    obj.value=List_tolist(GetFilterClaims());
    break;

  case "Group":
    SetFilterGroup(html_selected("filtergrp"));
    SetFilter("All");
    break;
    
  default:    
    SetFilter(selopt);
  }
   
  // changedfilters="yes";
  SetPrefFilter(GetDossierNumber(),GetFilterClaims(),GetClaimSetVersion());

  if(IsFilterChanged() || refresh){ 
    InitPageClaimIterator("flush");  
    eval(MOD_FTR._callback);      
  } 
  
  if(selopt=="_Find") {
    html_findstr(document.getElementById("f1").value);
  }
}


MOD_FTR.filtergoback=function(callback) {
  MOD_FTR._callback=callback;
  switch(MOD_FTR._prev_selfilter) {
  case "Find":
    MOD_FTR._filtercallback("_Find",MOD_FTR._prev_selfilterfind);
    break;
  case "List":
    MOD_FTR._filtercallback("_List",MOD_FTR._prev_selfilterclaims);
    break;
  default:
    MOD_FTR._filtercallback(MOD_FTR._prev_selfilter,MOD_FTR._prev_selfilterclaims);
  }
}

MOD_FTR.filteronclickfind=function(callback) {
  MOD_FTR._callback=callback;
  MOD_FTR._filtercallback("_Find",document.getElementById("f1").value,1);
}

MOD_FTR.onclickfind=function() {
  MOD_FTR._prev_selfilter="List";
  MOD_FTR._prev_selfilterclaims=MOD_FTR._selfilterclaims;  

  html_doselect("selfilter",MOD_FTR.CLSFILTERS,"Find");
	       
  document.getElementById("f1").className="SHOWME RWTXT";
  document.getElementById("f2").className="SHOWME";
  document.getElementById("f3").className="SHOWME";

  document.getElementById("o1").className="HIDEME";
  document.getElementById("o2").className="HIDEME";
}

MOD_FTR.onclickother=function() {
  MOD_FTR._prev_selfilter="Find";
  MOD_FTR._prev_selfilterclaims=MOD_FTR._selfilterclaims;  
  html_doselect("selfilter",MOD_FTR.CLSFILTERS,"List");

  document.getElementById("f1").className="HIDEME";
  document.getElementById("f2").className="HIDEME";
  document.getElementById("f3").className="HIDEME";

  document.getElementById("o1").className="SHOWME";
  document.getElementById("o2").className="SHOWME";
}


function filter_selection(callfunc,opts) {  
  var fclass,oclass;
  var html="<fieldset><legend>Filter</legend>";
  MOD_FTR._callback=callfunc;
  if(GetGroupsLength()>1) {
    html+="G "+html_group_selection("filtergrp",GetFilterGroup(),"MOD_FTR._filtercallback(\"Group\")");
  } 
  html+=" "+html_selection("selfilter",MOD_FTR.CLSFILTERS,GetFilter(),
			   "MOD_FTR._filtercallback("+(opts||"")+")");
  
  if(GetFilter()=="Find") {
    fclass="SHOWME RWTXT";
    oclass="HIDEME";
  } else {
    fclass="HIDEME";
    oclass="SHOWME";
  }
  // Find
  html+="<input type=text size=40 id=f1  CLASS='"+fclass+"'";
  html+=" value='"+GetFilterFind()+"' onchange='MOD_FTR._filtercallback(\"_Find\");'>";    
  html+=" <input type=image id=f2 src='"+findstr_img.src+"' CLASS='"+fclass+"'";
  html+="onclick='html_findstr(document.getElementById(\"f1\").value)'";
  html+=" ACCESSKEY='F' valign=top>";
  html+=" <input type=button align=right value='List' id=f3 ";
  html+=" onclick='MOD_FTR.onclickother();'  CLASS='"+fclass+"'>";

  // Other
  html+=" <input type=text size=40 name='selfilterclaims' id=o1 CLASS='"+oclass+"'";
  html+="  value='"+List_tolist(GetFilterClaims())+"' onchange='MOD_FTR._filtercallback(\"_List\");'>"; 
  html+=" <input type=button align=right value='Find' id=o2 onclick='MOD_FTR.onclickfind()';)' CLASS='"+oclass+"'>";

  // Always
  html+=" <input type=button value='*' onclick='MOD_FTR.onclickother();MOD_FTR._filtercallback(\"All\")'>";
  
  html+="</fieldset>";
  return html;
} 

function filter_doselect(filter,args,callback) {
  html_doselect("selfilter",MOD_FTR.CLSFILTERS,filter);
  var obj=document.getElementById("selfilterclaims");
  if(obj) {
    obj.value=args;
  }
  SetFilter(filter,args);
  if(IsFilterChanged()){ 
    InitPageClaimIterator();
    MOD_FTR._callback=callback;
    eval(callback);      
  } 
}

function InitFilters(filter,cls,opt) {
  SetFilter(filter,cls);
  InitPageClaimIterator();
  return;
}

// convert from array to claim list (comma separated claims), if tab empty return full list 
function filteredclaims() {
  var cllist= new List();
  for (var cl in MOD_FTR._CLSSHOW) {
    cllist.push(cl);
  }
  return cllist.getValue();
}

function filter_toTab() {
  var tab=[];
  for (var cl in MOD_FTR._CLSSHOW) {
    tab.push(cl);    
  }
  return tab;
}

function filteredclaims_iterator(cl) {
  this._iter= new claim_iterator(cl);
  this._next = function() { while(!this._iter.end()) {
      if(IsClaimInFilter(this._iter.getValue())) {
	return;
      }
      this._iter.next();
    }
  }
  this._next(); // init
}
filteredclaims_iterator.prototype.end      = function () { return this._iter.end();}
filteredclaims_iterator.prototype.next     = function () { this._iter.next(); this._next(); } 
filteredclaims_iterator.prototype.getValue = function () { return this._iter.getValue();}
filteredclaims_iterator.prototype.getText  = function()  { return this._iter.getText();}





